<?php
class dumper {
	
	function dumper() {
		$this->SET['tables'] = '';
		$this->tabs = 0;
		$this->records = 0;
		$this->size = 0;
		$this->comp = 0;
		
		$this->resultStr = "";
		
		set_time_limit(0);
		ignore_user_abort(true);
	}
	
	function fn_write( $str){
			$this->resultStr.=$str;
	}
	
	function fn_read(){
			if( $this->sqlLength>=4096)
			{
				$tempStr=substr($this->sqlStr,0,4096);
				$this->sqlStr=substr($this->sqlStr,4096);
				$this->sqlLength-=4096;
				return $tempStr;
			}
			else
			{
				$this->sqlLength=0;
				return $sqlStr;
			}
	}
	
	function fn_read_str(){
		$string = '';
		$this->file_cache = ltrim($this->file_cache);
		$pos = strpos($this->file_cache, "\n", 0);
		if ($pos < 1) {
			while (!$string && ($str = $this->fn_read())){
    			$pos = strpos($str, "\n", 0);
    			if ($pos === false) {
    			    $this->file_cache .= $str;
    			}
    			else{
    				$string = $this->file_cache . substr($str, 0, $pos);
    				$this->file_cache = substr($str, $pos + 1);
    			}
    		}
			if (!$str) {
			    if ($this->file_cache) {
					$string = $this->file_cache;
					$this->file_cache = '';
				    return trim($string);
				}
			    return false;
			}
		}
		else {
  			$string = substr($this->file_cache, 0, $pos);
  			$this->file_cache = substr($this->file_cache, $pos + 1);
		}
		return trim($string);
	}

	function backup() {
		$this->SET['tables_exclude'] = 1;
		$tables = array();
        $result = mysql_query("SHOW TABLES");
		$all = 0;
        while($row = mysql_fetch_array($result)) {
			$status = 0;
			if (!empty($tbls)) {
			    foreach($tbls AS $table){
    				$exclude = preg_match("/^\^/", $table) ? true : false;
    				if (!$exclude) {
    					if (preg_match("/^{$table}$/i", $row[0])) {
    					    $status = 1;
    					}
    					$all = 1;
    				}
    				if ($exclude && preg_match("/{$table}$/i", $row[0])) {
    				    $status = -1;
    				}
    			}
			}
			else {
				$status = 1;
			}
			if ($status >= $all) {
    			$tables[] = $row[0];
    		}
        }

		$tabs = count($tables);
		$result = mysql_query("SHOW TABLE STATUS");
		$tabinfo = array();
		$tabinfo[0] = 0;
		$info = '';
		while($item = mysql_fetch_assoc($result)){
			if(in_array($item['Name'], $tables)) {
				$item['Rows'] = empty($item['Rows']) ? 0 : $item['Rows'];
				$tabinfo[0] += $item['Rows'];
				$tabinfo[$item['Name']] = $item['Rows'];
				$this->size += $item['Data_length'];
				$tabsize[$item['Name']] = 1 + round(1 * 1048576 / ($item['Avg_row_length'] + 1));
				if($item['Rows']) $info .= "|" . $item['Rows'];
			}
		}
		$show = 10 + $tabinfo[0] / 50;
		$info = $tabinfo[0] . $info;
		$name = $db . '_' . date("Y-m-d_H-i");

		$t=0;

		$result = mysql_query("SET SQL_QUOTE_SHOW_CREATE = 1");

        foreach ($tables AS $table){
			$result = mysql_query("SHOW CREATE TABLE {$table}");
			if(mysql_error())echo "SHOW CREATE TABLE {$table}";
        	$tab = mysql_fetch_array($result);
			$tab = preg_replace('/(default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP|DEFAULT CHARSET=\w+|COLLATE=\w+|character set \w+|collate \w+)/i', '/*!40101 \\1 */', $tab);
        	$this->fn_write( "DROP TABLE IF EXISTS {$table};\n{$tab[1]};\n\n");

            $NumericColumn = array();
            $result = mysql_query("SHOW COLUMNS FROM {$table}");
            $field = 0;
            while($col = mysql_fetch_row($result)) {
            	$NumericColumn[$field++] = preg_match("/^(\w*int|year)/", $col[1]) ? 1 : 0;
            }
			$fields = $field;
            $from = 0;
			$limit = $tabsize[$table];
			$limit2 = round($limit / 3);
			if ($tabinfo[$table] > 0) 
			{
				$i = 0;
				
				
            	if(($result = mysql_query("SELECT * FROM {$table}")) && ($total = mysql_num_rows($result)))
            	{
            		while($row = mysql_fetch_row($result)) 
            		{
            			$this->fn_write("INSERT INTO `{$table}` VALUES");
						for($k = 0; $k < $fields; $k++)
						{
                    		if ($NumericColumn[$k])
                    		    $row[$k] = isset($row[$k]) ? $row[$k] : "NULL";
                    		else
                    			$row[$k] = isset($row[$k]) ? "'" . mysql_escape_string($row[$k]) . "'" : "NULL";
                    	}

    					$this->fn_write("\n(" . implode(", ", $row) . ");\n");
               		}
					mysql_free_result($result);					
            	}
				$this->fn_write( "\n\n");
			}
		}

		
		$this->tabs = $tabs;
		$this->records = $tabinfo[0];
		$this->comp = $this->SET['comp_method'] * 10 + $this->SET['comp_level'];
		
		return	$this->resultStr;
	}





function fn_int($num){
	return number_format($num, 0, ',', ' ');
}

function fn_arr2str($array) {
	$str = "array(\n";
	foreach ($array as $key => $value) {
		if (is_array($value)) {
			$str .= "'$key' => " . fn_arr2str($value) . ",\n\n";
		}
		else {
			$str .= "'$key' => '" . str_replace("'", "\'", $value) . "',\n";
		}
	}
	return $str . ")";
}






function restore($st){
	$len=strlen($st);
	$pr_char=' ';
	$tempSql='';
	$tabs=0;
	$rows=0;
	$in_const=false;
	for($i=0;$i<$len;$i++)
	{
		$char=$st[$i];
		if($char=="'" && $pr_char!="\\") $in_const=!$in_const;
		
		if($char!=';' || $in_const) $tempSql.=$char;
		else
		{
			$tempSql=ltrim(rtrim($tempSql));
			mysql_query($tempSql);
			if(strlen(mysql_error())>10) return mysql_error();
			if(strtolower(substr($tempSql,0,12))=='create table')	$tabs++;
			if(strtolower(substr($tempSql,0,6))=='insert')	$rows+=mysql_affected_rows();
			$tempSql='';
		}
		
		$pr_char=$char;
	}
	
	$this->tabs=$tabs;
	$this->records=$rows;
	return '';
}


}
?>